package com.zhongshengnetwork.rightbe.common;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;

//JsonMap实现
public class JsonMap<K, V> extends HashMap<K, V> {

    private static final long serialVersionUID = -4083915643878380063L;

    //从Json构造Map
    public static <K, V> JsonMap<K, V> fromJson(String json) {
        @SuppressWarnings("unchecked")
        JsonMap<K, V> map = JSON.parseObject(json, JsonMap.class);
        return map;
    }

    //重写toString方法
    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }


    //测试
    public static void main(String[] args) {
        JsonMap<String, String> jmap = new JsonMap<String, String>();
        jmap.put("hello", "world");
        jmap.put("你好", "世界");
        String jmapString = jmap.toString();
        System.out.println(jmap); //此处会自动调用toString()
        jmap = JsonMap.fromJson(jmapString);
        System.out.println(jmap.get("hello"));
    }

}
